import {timesFormat} from '@/util/util.js';
export class ClockIn {
	constructor(args) {
		
	}
	
	getData() {
		return new Promise((resolve, reject)=> {
			uni.getStorage({
				key: 'clockIn',
				complete: (res) => {
					let data = (res.data) ? JSON.parse(res.data) : res.data;
					
					resolve(data);
				}
			});
		});
	}
	
	setData(data) {
		return new Promise((resolve, reject) => {
			uni.setStorage({
				key: 'clockIn',
				data: JSON.stringify(data),
				success: () => {
					resolve();
				}
			});
		})
	}
	
	removeData() {
		return new Promise((resolve, reject) => {
			uni.removeStorage({
				key: 'clockIn',
				success: (res) => {
					resolve();
				}
			});
		})
	}
	async createData(checkType){
		let time = new Date().getTime();
		let timestamp = timesFormat(time, 'yyyyMMdd');
		let data = await this.getData();
		
		if(data) {
			if(data.date !== timestamp){
				data.date = '';
				data.payload = {};
			}
		} else {
			data = {
				date: '',
				payload: {}
			}
		}
		
		data.date = timestamp;
		data.payload.checkIn = checkType;
		
		return data;
	}
	async checkIn(type){
		let data = await this.createData(type);
		await this.setData(data);
	}
}